package com.ruoyi.web.controller.common.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.CommonImageDO;
import com.ruoyi.common.entity.VO.CommonImageVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.controller.common.service.IBackCommonImageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/back/commonImage")
public class BackCommonImageController extends BaseController {

    private String prefix = "common/image";

    @Resource
    private IBackCommonImageService backCommonImageService;

    /**
     * 页面-图片列表
     *
     * @return
     */
    @GetMapping("/list/{modelType}/{modelId}")
    public String list(@PathVariable("modelType") Integer modelType, @PathVariable("modelId") Long modelId, ModelMap mmap) {
        mmap.put("modelType",modelType);
        mmap.put("modelId",modelId);
        return prefix + "/list";
    }

    /**
     * 页面-新增图片
     *
     * @return
     */
    @GetMapping("/add/{modelType}/{modelId}")
    public String add(@PathVariable("modelType") Integer modelType, @PathVariable("modelId") Long modelId, ModelMap mmap) {
        mmap.put("modelType",modelType);
        mmap.put("modelId",modelId);
        return prefix + "/add";
    }

    /**
     * 页面-编辑图片
     *
     * @param imageId 图片ID
     * @return
     */
    @GetMapping("/edit/{imageId}")
    public String edit(@PathVariable("imageId") Long imageId, ModelMap mmap) {
        mmap.put("imageInfo", backCommonImageService.getImageInfoById(imageId));
        return prefix + "/edit";
    }


    /**
     * 数据查询-图片列表（搜索）
     *
     * @param commonImageDO
     * @return
     */
    @PostMapping("/searchList")
    @ResponseBody
    public TableDataInfo searchList(CommonImageDO commonImageDO) {
        startPage();
        List<CommonImageDO> list = backCommonImageService.searchList(commonImageDO);
        return getDataTable(list);
    }


    /**
     * 新增图片-数据
     */
    @PostMapping("/add")
    @Log(title = "总后台-多图管理", businessType = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult add(CommonImageVO commonImageVO) {
        try {
            commonImageVO.setCreateBy(ShiroUtils.getSysUser().getUserName());
            commonImageVO.setCreateTime(new Date());
            return AjaxResult.success("上传成功！",backCommonImageService.add(commonImageVO));
        } catch (BusinessException e) {
            return error(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return error("新增图片失败！");
        }
    }

    /**
     * 编辑图片-数据
     */
    @PostMapping("/edit")
    @Log(title = "总后台-多图管理", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult edit(CommonImageDO commonImageDO) {
        try {
            commonImageDO.setUpdateBy(ShiroUtils.getSysUser().getUserName());
            commonImageDO.setUpdateTime(new Date());
            backCommonImageService.edit(commonImageDO);
            return success();
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除图片-数据处理
     *
     * @param ids 图片ID拼接字符串
     * @return
     */
    @PostMapping("/remove")
    @Log(title = "多图管理", businessType = BusinessType.DELETE)
    @ResponseBody
    public AjaxResult delete(String ids) {
        return toAjax(backCommonImageService.remove(ids));
    }
}
